#include <iostream>
#include <vector>
#include <utility>
#include <climits>
#include <algorithm>
using namespace std;

pair<int,int> helper(const vector<int>& v,const int& minVal,const int& maxVal)
{
    pair<int,int> p;
    int left = 0,right = v.size()-1;
    int mid = 0;
    while(left <= right)
    {
        mid = left + (right-left)/2;
        if(v[mid] < minVal){
            left = mid + 1;
        }
        else{
            p.first = mid;
            right = mid - 1;
        }
    }
    left = 0,right = v.size()-1;
    while(left <= right)
    {
        mid = left + (right-left)/2;
        if(v[mid] > maxVal){
            right = mid - 1;
        }
        else{
            p.second = mid;
            left = mid + 1;
        }
    }
    return p;
}
int main()
{
    int n = 0,p = 0;
    cin >> n >> p;
    vector<int> v(n);
    for(int i = 0;i < n;++i)
    {
        cin >> v[i];
    }
    int res = INT_MIN;
    std::sort(v.begin(),v.end());
    int Max =  v[v.size()-1];
    for(int i = 1;i <= Max;++i)
    {
        pair<int,int> p1 = helper(v,i-p,i+p);
        res = std::max(res,(p1.second - p1.first) + 1);
    }
    cout << res;
    
    return 0;
}